package com.crossdomain.oauth2.client.common.service;

import java.util.Hashtable;
import java.util.Map;

import javax.servlet.http.Cookie;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import com.crossdomain.oauth2.client.common.util.RSAUtils;

public class K1kCookieService {

	private static final String X_AUTH_SOURCE_KEY = "x-auth-source";
	private static final String LOGIN_FROM_KEY = "loginFrom";
	private static final String USERNAME = "username";
	private static final String PASSWORD = "password";
	private static final String SETCOOKIE = "Set-Cookie";
	private static final String AUTHTOKEN = "AUTHTOKEN";
	private static final String DOMAIN = "Domain";
	private static final String PATH = "Path";

	public Cookie login(String loginUrl, String userId, String base64StringKeyPair, String authSourceType,
			String loginType) throws Exception {
		RestTemplate restTemplate = new RestTemplate();
		HttpHeaders requestHeaders = new HttpHeaders();
		requestHeaders.add(X_AUTH_SOURCE_KEY, authSourceType);
		HttpEntity<Void> httpEntity = new HttpEntity<Void>(null, requestHeaders);

		String sign = RSAUtils.encryptString(RSAUtils.getPublicKey(base64StringKeyPair), userId);

		String url = loginUrl + "?" + LOGIN_FROM_KEY + "=" + loginType + "&" + USERNAME + "=" + userId + "&" + PASSWORD
				+ "=" + sign;

		ResponseEntity<Void> loginResponse = restTemplate.postForEntity(url, httpEntity, Void.class);
		return this.assembleCookie(loginResponse);
	}

	private Cookie assembleCookie(ResponseEntity<Void> loginResponse) throws Exception {
		if (loginResponse.getStatusCode() == HttpStatus.OK) {
			String result = loginResponse.getHeaders().getFirst(SETCOOKIE);

			Map<String, String> map = new Hashtable<String, String>();
			String[] arr = result.split(";");
			for (String item : arr) {
				String[] kv = item.split("=");
				map.put(kv[0].trim(), kv[1].trim());
			}

			Cookie cookie = new Cookie(AUTHTOKEN, map.get(AUTHTOKEN));
			cookie.setDomain(map.get(DOMAIN));
			cookie.setPath(map.get(PATH));

			return cookie;
		} else {
			throw new Exception("request login fail.");
		}
	}

	public static void main(String[] args) {

		K1kCookieService service = new K1kCookieService();
		String loginUrl = "http://rz-pt.kao1kao.com/rz-pt/login";
		String userId = "8607665";
		String base64StringKeyPair = "rO0ABXNyABVqYXZhLnNlY3VyaXR5LktleVBhaXKXAww60s0SkwIAAkwACnByaXZhdGVLZXl0ABpMamF2YS9zZWN1cml0eS9Qcml2YXRlS2V5O0wACXB1YmxpY0tleXQAGUxqYXZhL3NlY3VyaXR5L1B1YmxpY0tleTt4cHNyADFvcmcuYm91bmN5Y2FzdGxlLmpjZS5wcm92aWRlci5KQ0VSU0FQcml2YXRlQ3J0S2V5bLqHzgJzVS4CAAZMAA5jcnRDb2VmZmljaWVudHQAFkxqYXZhL21hdGgvQmlnSW50ZWdlcjtMAA5wcmltZUV4cG9uZW50UHEAfgAFTAAOcHJpbWVFeHBvbmVudFFxAH4ABUwABnByaW1lUHEAfgAFTAAGcHJpbWVRcQB+AAVMAA5wdWJsaWNFeHBvbmVudHEAfgAFeHIALm9yZy5ib3VuY3ljYXN0bGUuamNlLnByb3ZpZGVyLkpDRVJTQVByaXZhdGVLZXlG6wnAB89BHAMAA0wAC2F0dHJDYXJyaWVydAA9TG9yZy9ib3VuY3ljYXN0bGUvamNlL3Byb3ZpZGVyL1BLQ1MxMkJhZ0F0dHJpYnV0ZUNhcnJpZXJJbXBsO0wAB21vZHVsdXNxAH4ABUwAD3ByaXZhdGVFeHBvbmVudHEAfgAFeHBzcgAUamF2YS5tYXRoLkJpZ0ludGVnZXKM/J8fqTv7HQMABkkACGJpdENvdW50SQAJYml0TGVuZ3RoSQATZmlyc3ROb256ZXJvQnl0ZU51bUkADGxvd2VzdFNldEJpdEkABnNpZ251bVsACW1hZ25pdHVkZXQAAltCeHIAEGphdmEubGFuZy5OdW1iZXKGrJUdC5TgiwIAAHhw///////////////+/////gAAAAF1cgACW0Ks8xf4BghU4AIAAHhwAAAAELMww08GK0BjShSeTitUvbN4c3IAE2phdmEudXRpbC5IYXNodGFibGUTuw8lIUrkuAMAAkYACmxvYWRGYWN0b3JJAAl0aHJlc2hvbGR4cD9AAAAAAAAIdwgAAAALAAAAAHhzcgAQamF2YS51dGlsLlZlY3RvctmXfVuAO68BAwADSQARY2FwYWNpdHlJbmNyZW1lbnRJAAxlbGVtZW50Q291bnRbAAtlbGVtZW50RGF0YXQAE1tMamF2YS9sYW5nL09iamVjdDt4cAAAAAAAAAAAdXIAE1tMamF2YS5sYW5nLk9iamVjdDuQzlifEHMpbAIAAHhwAAAACnBwcHBwcHBwcHB4c3EAfgAJ///////////////+/////gAAAAF1cQB+AA0AAAAQlT7jHVmdApWG+/0Fd2jxiXh4c3EAfgAJ///////////////+/////gAAAAF1cQB+AA0AAAAIKrsV762JJpR4c3EAfgAJ///////////////+/////gAAAAF1cQB+AA0AAAAIB3Jzfq19Ygl4c3EAfgAJ///////////////+/////gAAAAF1cQB+AA0AAAAIhXtXqyxQr7V4c3EAfgAJ///////////////+/////gAAAAF1cQB+AA0AAAAI+osprRLuly94c3EAfgAJ///////////////+/////gAAAAF1cQB+AA0AAAAItxfKU4f64L14c3EAfgAJ///////////////+/////gAAAAF1cQB+AA0AAAADAQABeHNyAC1vcmcuYm91bmN5Y2FzdGxlLmpjZS5wcm92aWRlci5KQ0VSU0FQdWJsaWNLZXklImoOW/pshAIAAkwAB21vZHVsdXNxAH4ABUwADnB1YmxpY0V4cG9uZW50cQB+AAV4cHEAfgAMcQB+ACI=";
		String authSourceType = "UP_WSYPT";
		String loginType = "wsypt";
		try {
			Cookie cookie = service.login(loginUrl, userId, base64StringKeyPair, authSourceType, loginType);
			System.out.println(cookie.getValue());
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
